home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / programming / c / bitmap24 / bm24test.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-17  |  4.5 KB  |  174 lines

  1. /* -------------------------------------------------------------------------- *\
  2.    BM24TEST.CPP, a program to demonstrate the BitMap24 class
  3.    Copyright (C) 1999  Jarno van der Linden
  4.    jarno@kcbbs.gen.nz
  5.  
  6.    This program is free software; you can redistribute it and/or
  7.    modify it under the terms of the GNU General Public License
  8.    as published by the Free Software Foundation; either version 2
  9.    of the License, or (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  
  20.  
  21.    01 May 1999: Project started
  22. \* -------------------------------------------------------------------------- */
  23.  
  24. /* -------------------------------- Includes -------------------------------- */
  25. #include <iostream.h>
  26.  
  27. #include <stdlib.h>
  28.  
  29. #include "bitmap24.h"
  30. #include "zbitmap24.h"
  31.  
  32. /* ------------------------------ Definitions ------------------------------- */
  33.  
  34. /* --------------------------------- Macros --------------------------------- */
  35.  
  36. /* -------------------------------- Typedefs -------------------------------- */
  37.  
  38. /* ------------------------------ Proto Types ------------------------------- */
  39.  
  40. /* -------------------------------- Structs --------------------------------- */
  41.  
  42. /* -------------------------------- Globals --------------------------------- */
  43.  
  44. /* ---------------------------------- Code ---------------------------------- */
  45.  
  46. /*
  47.  * A simple example of bitmap reading
  48.  * The bitmap is read from the given filename
  49.  * Just outputs some info about the bitmap
  50.  *
  51.  */
  52. void ReadTest(char *filename)
  53. {
  54.     BitMap24 *inputbm;
  55.  
  56.     cout << "ReadTest" << endl;
  57.     cout << "Reading bitmap, please wait (and wait, and wait,...)" << endl;
  58.  
  59.     inputbm = new BitMap24( filename );
  60.  
  61.     if( ! inputbm->HasError() )
  62.     {
  63.         cout << "Name: "          << filename                 << endl;
  64.         cout << "BitMap Width: "  << inputbm->GetWidth()      << endl;
  65.         cout << "BitMap Height: " << inputbm->GetHeight()     << endl;
  66.         cout << "Image Width: "   << inputbm->GetRealWidth()  << endl;
  67.         cout << "Image Height: "  << inputbm->GetRealHeight() << endl;
  68.     }
  69.  
  70.     cout << "Error: " << inputbm->GetErrorStr() << endl << endl;
  71.  
  72.     delete inputbm;
  73. }
  74.  
  75.  
  76. /*
  77.  * A simple example of bitmap reading and writing
  78.  * A bitmap is read from the given filename
  79.  * A new bitmap is created of half the size by
  80.  * skipping every second column and row.
  81.  * (This is not the way a scaling should be done)
  82.  * The resulting bitmap is saved to T:bm24out.iff24
  83.  *
  84.  */
  85. void WriteTest(char *infilename)
  86. {
  87.     BitMap24 inbm, outbm;
  88.     int x,y;
  89.     Colour inc;
  90.  
  91.     cout << "WriteTest" << endl;
  92.     cout << "Reading bitmap, please wait (and wait, and wait,...)" << endl;
  93.  
  94.     inbm.ReadBitMap(infilename);
  95.     if(inbm.HasError())
  96.     {
  97.         cout << "Error: " << inbm.GetErrorStr() << endl;
  98.         return;
  99.     }
  100.  
  101.     outbm.SetSize(inbm.GetWidth()/2,inbm.GetHeight()/2);
  102.  
  103.     cout << "Scaling..." << endl;
  104.  
  105.     for(y=0; y<outbm.GetHeightFast(); y++)
  106.     {
  107.         for(x=0; x<outbm.GetWidthFast(); x++)
  108.         {
  109.             inbm.GetColourFast(&inc, x*2,y*2);
  110.             outbm.SetColourFast(inc,x,y);
  111.         }
  112.     }
  113.  
  114.     cout << "Writing..." << endl;
  115.  
  116.     outbm.WriteBitMap("T:bm24out.iff24");
  117.  
  118.     cout << "Error: " << outbm.GetErrorStr() << endl << endl;
  119. }
  120.  
  121.  
  122. /*
  123.  * A simple example of using ZBitMap24
  124.  * A small bitmap is filled, and written
  125.  * to T:zbm24out.iff24
  126.  *
  127.  */
  128. void ZTest(void)
  129. {
  130.     ZBitMap24 bm;
  131.     int x,y;
  132.  
  133.     cout << "ZTest" << endl;
  134.  
  135.     bm.SetSize(40,60);            // Bitmap is 40 wide, 60 high
  136.     bm.SetMaxZ(1.0);            // Indicate at least the largest
  137.                                 // depth value we are going to write
  138.  
  139.     if(bm.HasError())
  140.     {
  141.         cout << "Error: " << bm.GetErrorStr() << endl;
  142.         return;
  143.     }
  144.  
  145.     cout << "Filling..." << endl;
  146.  
  147.     for(y=0; y<bm.GetHeightFast(); y++)
  148.     {
  149.         for(x=0; x<bm.GetWidthFast(); x++)
  150.         {
  151.             bm.SetColourFast(255,0,0,((double)x)/bm.GetWidthFast(),x,y);
  152.             bm.SetColourFast(0,255,0,((double)y)/bm.GetHeightFast(),x,y);
  153.             if(bm.CanDrawSet(x,y,0.5))            // Just to be different...
  154.                 ((BitMap24)bm).SetColourFast(0,0,255,x,y);
  155.         }
  156.     }
  157.  
  158.     cout << "Writing..." << endl;
  159.  
  160.     bm.WriteBitMap("T:zbm24out.iff24");
  161.  
  162.     cout << "Error: " << bm.GetErrorStr() << endl;
  163. }
  164.  
  165.  
  166. int main( int argc, char *argv[] )
  167. {
  168.     ReadTest( argc == 2 ? argv[1] : "PROGDIR:Basket.iff24" );
  169.     WriteTest( argc == 2 ? argv[1] : "PROGDIR:Basket.iff24" );
  170.     ZTest();
  171.  
  172.     return EXIT_SUCCESS;
  173. }
  174.